<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <div class="userInfo">
              <!-- 新增角色按钮 -->
              <el-row style="height: 60px">
                <el-button
                  icon="el-icon-plus"
                  size="small"
                  type="primary"
                  @click="hAdd()"
                >新增角色</el-button>
              </el-row>
              <!-- 表格 -->

              <el-table :data="roles" border>
                <el-table-column
                  label="序号"
                  type="index"
                  :index="getIndex"
                  width="50"
                />
                <el-table-column label="角色名称" prop="name" width="180" />
                <el-table-column label="描述" prop="description" />
                <el-table-column label="操作" width="300">
                  <template slot-scope="scope">
                    <el-button
                      size="small"
                      type="success"
                      @click="hAssign(scope.row.id)"
                    >分配权限</el-button>
                    <el-button
                      size="small"
                      type="primary"
                      @click="edit(scope.row)"
                    >编辑</el-button>
                    <el-button
                      size="small"
                      type="danger"
                      @click="del(scope.row.id)"
                    >删除</el-button>
                  </template>
                </el-table-column>
              </el-table>

              <!-- 分页 -->
              <el-row
                type="flex"
                justify="center"
                align="middle"
                style="height: 60px"
              >
                <!-- 分页组件 -->
                <el-pagination
                  background
                  :page-sizes="[1, 2, 3]"
                  :current-page="params.page"
                  :page-size="params.pagesize"
                  :total="total"
                  layout="total, sizes, prev, pager, next, jumper"
                  @current-change="hCurrentChange"
                  @size-change="hSizeChange"
                />
              </el-row>
            </div>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <el-dialog
        :title="isEdit ? '编辑' : '添加'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
        @close="resetForm"
      >
        <el-form
          ref="roleForm"
          :model="roleForm"
          :rules="rules"
          label-width="100px"
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button
              size="medium"
              @click="showDialog = false"
            >取消</el-button>
            <el-button
              size="medium"
              type="primary"
              @click="hSubmit"
            >确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>

      <el-dialog

        title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
        :visible.sync="showDialogAssign"
      >
        <AssignPermission v-if="showDialogAssign" :cur-id="id" @close="showDialogAssign=false" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRoles, delRoles, addRoles, editRoles } from '@/api/setting'
import AssignPermission from './AssignPermission'

export default {
  components: {
    AssignPermission
  },
  data() {
    return {
      isEdit: false, // 是否编辑
      showDialog: false, // 是否显示弹框
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: []
      },
      roles: [],
      params: {
        page: 1,
        pagesize: 2
      },
      total: 0,
      showDialogAssign: false,
      id: ''
    }
  },
  computed: {
    maxPage() {
      return Math.ceil(this.total / this.params.pagesize)
    },
    isLastPage() {
      return this.total % this.params.pagesize === 0
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    async loadRoles() {
      try {
        const res = await getRoles(this.params)
        // console.log(res)
        this.roles = res.data.rows
        this.total = res.data.total
      } catch (err) {
        console.log(err)
      }
    },
    // 增
    hAdd() {
      this.showDialog = true
      this.isEdit = false
      this.doAdd()
    },
    async doAdd() {
      const res = await addRoles(this.roleForm)
      console.log(res)
      // 跳转到最后一页
      if (this.isLastPage) {
        this.params.page = this.maxPage + 1
        // 为了让页码显示正确
        this.total++
      } else {
        this.params.page = this.maxPage
      }
      // this.params.page = Math.ceil(++this.total / this.params.pagesize)

      // 重新加载
      this.loadRoles()
      // 提示成功
      this.$message.success('添加成功')
      // 关闭弹窗
      this.showDialog = false
    },
    // 删
    del(id) {
      this.$confirm('你确定要删除么?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.doDel(id)
        })
        .catch((err) => {
          console.log(err)
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    async doDel(id) {
      try {
        const res = await delRoles(id)
        console.log(res)
        this.$message.success('删除成功')

        if (this.roles.length === 1) {
          this.params.page--
          if (this.params.page <= 0) {
            this.params.page = 1
          }
        }

        this.loadRoles()
      } catch (err) {
        console.log(err)
        this.$message.Error('删除失败')
      }
    },
    // 改
    edit(row) {
      console.log(row)
      // 弹框
      this.showDialog = true
      // 数据回填
      // 深拷贝
      this.roleForm = { ...row }

      this.isEdit = true
    },
    async doEdit() {
      try {
        const res = await editRoles(this.roleForm)
        console.log(res)
        this.loadRoles()
        this.$message.success('编辑成功')
        this.showDialog = false
      } catch (err) {
        this.$message.error('编辑失败')
      }
    },
    // 分配权限
    hAssign(id) {
      this.showDialogAssign = true
      this.id = id
    },
    // 分页
    hCurrentChange(curPage) {
      this.params.page = curPage
      this.loadRoles()
    },
    hSizeChange(curPage) {
      this.params.pagesize = curPage
      this.loadRoles()
    },
    // 分页序号延续
    getIndex(index) {
      const page = this.params.page
      const pagesize = this.params.pagesize
      // 当前页数 - 1 * 每页数据条数 + index + 1
      return index + 1 + (page - 1) * pagesize
    },
    // 确定按钮
    hSubmit() {
      // 表单校验
      // 做具体的添加动作
      // 发起请求
      if (this.isEdit) {
        this.doEdit()
      } else {
        this.doAdd()
      }
    },
    // 关闭弹窗重置数据
    resetForm() {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
    }
  }
}
</script>

<style scope>
.userInfo {
  padding: 30px;
}
</style>
